package test;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.http.HttpUtils;

import org.junit.Test;

import vc.bean.ResourceBean;
import vc.bean.SearchHits;

import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.common.json.JSONArray;
import com.alibaba.dubbo.common.json.JSONObject;
import com.alibaba.dubbo.common.json.ParseException;

public class testSearchData {

	/*
	 * {
	 * "resourceBean":{
	 *   "id":"e6f125865e4322a13e7267325cc2728a",
	 *   "rating":41.8,
	 *   "title":"Zebra, graphic eye detail",
	 *   "url":"https://drscdn.500px.org/photo/55161944/w%3D280_h%3D280/e37ba9592dcd08bb4f0681dedd727bb9?v=0"
	 *  },
	 *  "score":1,
	 *  "source":{
	 *             "src_url":"https://drscdn.500px.org/photo/55161944/m%3D2048/22e858ebee04e3197344f49ebd19cd6b",
	 *             "licence_id":0,
	 *             "rating":41.8,
	 *             "resource_type":2,
	 *             "width":900,
	 *             "description":"Zebra, graphic eye detail",
	 *             "created_date":"2012-05-29 14:05:26",
	 *             "tag":["animal","art","artistic","bitmap","black","black and white","design","detail","dynamic","eye","head","line","look","mammal","pattern","raster","stripe","white","zebra","zoo"],
	 *             "title":"Zebra, graphic eye detail",
	 *             "user":"Tom Karola",
	 *             "url":"https://drscdn.500px.org/photo/55161944/w%3D280_h%3D280/e37ba9592dcd08bb4f0681dedd727bb9?v=0",
	 *             "height":738
	 *  }
	 * }
	 */
	
	@Test
	public void test() throws IOException, ParseException {
        //String url_path = "http://photo-test-community.shijue.me/search/search.html?category=0&orderby=0&license=-1&key=&size=100&type=json";  
        URL url = new URL("http://photo-test-community.shijue.me/search/search.html?category=0&orderby=0&license=-1&key=&size=100&type=json");
        URLConnection urlConnection = url.openConnection();
        HttpURLConnection connection = null;
        if(urlConnection instanceof HttpURLConnection)
        {
           connection = (HttpURLConnection) urlConnection;
        }
        else
        {
           System.out.println("Please enter an HTTP URL.");
           return;
        }
        BufferedReader in = new BufferedReader(
        new InputStreamReader(connection.getInputStream()));
        String urlString = "";
        String current;
        while((current = in.readLine()) != null)
        {
           urlString += current;
        }
        //System.out.println(urlString);
        JSONObject jsonObj=(JSONObject) JSON.parse(urlString);
        
        JSONArray jArray=(JSONArray) jsonObj.get("list");
        
        System.out.println();
        for(int i=0;i<jArray.length();i++){
        	//ResourceBean rb=JSON.parse(JSON.json(jArray.get(i).get("resourceBean")),ResourceBean.class);
        	JSONObject rowObj=(JSONObject) jArray.get(i);
        	JSONObject resourceBeanObj=(JSONObject) rowObj.get("resourceBean");
        	//with resourceBeanObj we can get "id","title","url"
        	//System.out.println(resourceBeanObj.getString("id"));
        	JSONObject sourceObj=(JSONObject) rowObj.get("source");
        	//With sourceObj, we can get "licence_id","resource_type","description","created_date","tag"."title","user","height",
        }
        
        
        //SearchHits sh=new SearchHits();
        //SearchHits sh=JSON.parse(urlString,SearchHits.class);
        //System.out.println(sh.getTotal());
        
   }

}
